#ifndef _OHCI_H
#define _OHCI_H 1
#include "usb.h"


#define HcRevision 	0
#define HcControl		4
#define 			ControlBulkServiceRatio  BIT0 + BIT1
#define			PeriodicListEnable		BIT2
#define			IsochronousEnable		BIT3
#define			ControlListEnable		BIT4
#define			BulkListEnable			BIT5

#define HcCommandStatus 8
#define			HostControllerReset		BIT0
#define			ControlListFilled			BIT1
#define			BulkListFilled			BIT2
#define			OwnershipChangeRequest BIT3
#define HcInterruptStatus 0x0c
#define			WriteBackDoneHead		BIT1
#define			RootHubStatusChange	BIT6
#define HcInterruptEnable 0x10
#define HcInterruptDisable 0x14
#define HcHCCA 			  0x18
#define HcPeriodCurrentED  0x1c
#define HcControlHeadED 	0x20
#define HcControlCurrentED 0x24
#define HcBulkHeadED  	0x28 
#define HcBulkCurrentED 	0x2c
#define HcDoneHead 		0x30
#define HcFmInterval 	0x34
#define HcFmRemaining	0x38
#define HcFmNumber 		0x3c
#define HcPeriodicStart	0x40
#define HcLSThreshold	0x44
#define HcRhDescriptorA	0x48
#define			NumberDownstreamPorts		BIT0+BIT1+BIT2+BIT3
#define			PowerSwitchingMode		BIT8
#define HcRhDescriptorB 	0x4c
#define HcRhStatus 		0x50
#define			SetGlobalPower			BIT16
#define HcRhPort1Status 	0x54
#define			CurrentConnectStatus  	BIT0
#define			PortEnableStatus		BIT1
#define			SetPortEnable			BIT1
#define			PortSuspendStatus		BIT2
#define			SetPortSuspend			BIT2
#define			PortOverCurrentIndicator BIT3
#define			ClearSuspendStatus		BIT3
#define			PortResetStatus			BIT4
#define			SetPortReset			BIT4
#define			PortPowerStatus			BIT8
#define			SetPortPower			BIT8
#define			LowSpeedDeviceAttached BIT9
#define			ClearPortPower			BIT9
#define			ConnectStatusChange	BIT16
#define			PortEnableStatusChange  BIT17
#define			PortSuspendStatusChange BIT18
#define			PortOverCurrentIndicatorChange BIT19
#define			PortResetStatusChange	BIT20



#define 	PIDSETUP 0
#define   PIDOUT	 1
#define   PIDIN	2
#define   NOINTR  7
#define	ToggleCarry   0
#define	Data0Tg   2
#define	Data1Tg   3
#define	SKIP_ED		BIT14

#define 	TransationTime 100

typedef struct ED{
//	DWORD  FunctionAddress:7;
//	DWORD  EndPoint:4;
//	DWORD  Directoin:2;
//	DWORD  Speed:1;
//	DWORD  sKip:1;
//	DWORD  Format:1;
//	DWORD  MaxPacketSize:11;
//	DWORD  reserverd:5;
	DWORD  Control;
	DWORD  TailP;
	DWORD  HeadP;
	DWORD  NextED;
}EDType;

typedef struct OHCITD{
//reserve:18
//BufferRounding:1
//PID:2
//DelayInterrupt:3
//DataToggle:2
//ErrorCount:2
//ConditionCode:4
	DWORD  Control;
	DWORD  CurrentBufferP;
	DWORD  NextTD;
	DWORD  BufferEnd;
}OHCITDType;

typedef struct {
	DWORD		InterruptTable[32]; //HccaInterrruptTable
	WORD		wFrameNumber;
	WORD		wPad1;
	DWORD		dwDoneHead;
	DWORD		bufferTemp[30];//reserve
}HCCAType;
	

typedef  
struct OHCIDescriptor{
//hcca...
	HCCAType	HCCA; //256-aligned

//bulk data...
	OHCITDType	BulkData;
	EDType		BulkED;

//interrupt data...
	EDType		ED32ms;
	EDType		ED16ms;	
	EDType		ED8ms;		
	EDType		ED4ms;		
	EDType		ED2ms;		
	EDType		ED1ms;		
	
//control data...
	OHCITDType	ControlSetup;
	OHCITDType	ControlData;
	OHCITDType	ControlStatus;
	OHCITDType  NullTD;
	EDType		ControlED;
		

	BYTE		buffer[256];	
}OHCIDescriptorType;



/******************************************************************************
    Function:
        OHCI_ControllerInitial

    Description:
	initialize OHCI controller.

    Entry:
	pHC   pointer to controller description

    Exit:
        none
******************************************************************************/
void OHCI_ControllerInitial(void * pHC);


/******************************************************************************
    Function:
        OHCI_ControlTransfer

    Description:
	OHCI control tansfer type

    Entry:
	ptr --  device description
	RFPtr -- request data
    Exit:
        status    1---stalled  ; 0 ----ok.
    Note: 
          if want to transmit data , please place it in UHCIDescriptor.buffer1;
          receve data after transation also put it in UHCIDescriptor.buffer1;
******************************************************************************/
BYTE  OHCI_ControlTransfer(DeviceInfoPtr ptr,  RequstFormatType * RFPtr);
/******************************************************************************
    Function:
        OHCI_bulkTransfer

    Description:
	OHCI bulk tansfer type

    Entry:
	DIPtr --  device description
	direction -- data transfer direction
       bufferPtr: data buffer to send or received
       length-- data length
       ToggleValue--refer to usb2.0 8.5.2. For bulk read, buld write, data begins with 0, alter each success transfer.
       TransLength-- actual transfer length, byte unit.
	
    Exit:
        status    0 ----ok.
                    bit0: STALL
                    bit1: time out
                    bit2: error

******************************************************************************/
BYTE OHCI_bulkTransfer(DeviceInfoPtr DIPtr, BYTE direction, BYTE * bufferPtr,WORD length,WORD *TransLength);
/******************************************************************************
    Function:
        OHCI_interruptTransfer

    Description:
	OHCI bulk tansfer type

    Entry:
	DIPtr --  device description
	direction -- data transfer direction
       bufferPtr: data buffer to send or received
       length-- data length
       TransLength-- actual transfer length, byte unit.
	
    Exit:


******************************************************************************/
BYTE OHCI_interruptTransfer(DeviceInfoPtr DIPtr, BYTE direction, BYTE * bufferPtr,WORD length,WORD *TransLength);
BYTE * OHCI_pointToDevBuffer(DeviceInfoPtr ptr);
void OHCI_DevPortInit(DeviceInfoPtr ptr,BYTE portIndex);

ControllerAccessMethodType * getOHCIMethod();

#endif
